﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Web.Security;

using Ample.BLL.Store;
using Ample.Model;

public partial class Users_OrderHistory : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if(!Page.IsPostBack)
        {
            BindOrderHistory();
        }
    }

    private void BindOrderHistory()
    {
        OrderBO obo = new OrderBO();

        gvOrderList.DataSource = obo.GetOrdersRecentStatusByUsername(this.Profile.UserName);
        gvOrderList.DataBind();
    }


    protected void gvOrderList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Select")) 
        {
            OrderBO obo = new OrderBO();
            Order order = obo.GetOrderById(Int32.Parse(e.CommandArgument.ToString()));

            ltlOrderId.Text = string.Format("#{0}",order.OrderNo.ToString());
            ltlAmpleID.Text = this.Profile.UserName;
            ltlAddedDate.Text = order.AddedDate.ToString();
            ltlUserName.Text = this.Profile.FirstName + " " + this.Profile.LastName;

            gvwOrderDetails.DataSource = order.OrderItems;
            gvwOrderDetails.DataBind();

            ltlItemTotal.Text = order.SubTotal.ToString("C");

            ltlInvoiceTotal.Text = order.SubTotal.ToString("C");
            ltlPaymentsRecieved.Text = "$0";
            ltlBalance.Text = order.SubTotal.ToString("C"); 

            pnlOrderDetails.Visible = true;
            pnlOrderDetails.CssClass = "orderdetails";
        }
        else if (e.CommandName.Equals("CancelDelete"))
        {
            int orderID = Int32.Parse(e.CommandArgument.ToString());
            OrderBO.DeleteOrder(orderID);

            BindOrderHistory();
            pnlOrderDetails.Visible = false;
        }
    }

    protected void gvOrderList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
         if (e.Row.RowType == DataControlRowType.DataRow)
         {
             Order order = (Order) e.Row.DataItem;

             if ( order.Status != null && order.Status.Equals(Ample.DAL.Store.PaymentStatus.Pending.ToString()))
                 e.Row.FindControl("lBtnCancel").Visible = true; 
         }
    }
}
